package @{daoPkg};

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import @{daoPkg}.BaseDao;
import com.zzwtec.interfaces.Page;
import @{modelPkg}.@{EntityModel};

@Repository(value = "@{entityModel}Dao")
public class @{EntityModel}Dao extends BaseDao<@{EntityModel}>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	private final String TABLE_NAME = "@{entity_model}";
	
	
	/**
	 * 分页查询
	 * @param page
	 * @return 数据模型列表
	 */
	@Transactional(readOnly = true)
	public Page<@{EntityModel}> selectPage(Page<@{EntityModel}> page) {
		return super.selectPage(@{EntityModel}.class, TABLE_NAME, page, new @{EntityModel}RowMapper());
	}
	
	/**
	 * 全查询，一般不推荐使用，除非数据量不大
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<@{EntityModel}> selectAll() {		
		return super.selectAll(TABLE_NAME, new @{EntityModel}RowMapper());
	}

	/**
	 * 获取一条记录
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public @{EntityModel} selectOne(String id) {		
		return super.selectOne(TABLE_NAME, id, new @{EntityModel}RowMapper());

	}

	

	

	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @return
	 */
	public int update(String sql) {
		return jdbcTemplate.update(sql);
	}

	/**
	 * 批量更新单表数据
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql) {
		return jdbcTemplate.batchUpdate(sql);
	}

	/**
	 * 更新区域
	 * @param bean
	 * @return
	 */
	public int update(@{EntityModel} bean) {
		String sql = "@{updateByPKSql}"; 
		Object[] params = @{updateByPKSqlParam};
		return update(sql, params);
	}
	
	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int update(String sql, Object... params) {
		return jdbcTemplate.update(sql, params);
	}

	/**
	 * 批量更新单表数据,支持sql预处理语句参数
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return jdbcTemplate.batchUpdate(sql, batchArgs);
	}

	/**
	 * 添加区域
	 * @param bean
	 * @return
	 */
	public int insert(@{EntityModel} bean) {
		String sql = "@{insertSql}";
		Object[] params = @{insertSqlParam};		
		return insert(sql, params);
	}
	
	

	/**
     * 按id删除数据
     * @param id
     * @return
     */
	public int deleteById(String id) {
		String sql = "delete from @{tableName} where id = ?";		
		Object[] params = new Object[]{id};
		return delete(sql, params);
	}
	
	/**
	 * 按ids删除数据
     * @param ids
     * @return 受影响行数
	 */
	public int deleteByIds(String[] ids) {
		return super.deleteByIds(TABLE_NAME, ids);
	}
	
	/**
	 * 删除数据 
	 * @param sql
	 * @param params
	 * @return
	 */
	private int delete(String sql, Object... params) {
		if (sql == null || !sql.startsWith("delete")) {
			throw new RuntimeException("不是合法的删除数据sql语句: " + sql);
		}
		return jdbcTemplate.update(sql, params);
	}

}

class @{EntityModel}RowMapper implements RowMapper<@{EntityModel}> {
	@Override
	public @{EntityModel} mapRow(ResultSet rs, int rowNum) throws SQLException {
		@{EntityModel} bean = new @{EntityModel}();		
		<? for(field in fields){ 
			var name = field.name;
			name = str.capitalize(name);
			var type = field.javaType;
			if(type == 'Integer'){
				type = 'Int';
			}			
		?>
		bean.set@{name}(rs.get@{type}("@{field.column}"));
		<?}?>
		return bean;
	}

}